package tree;

import java.util.Scanner;


/**使用孩子-兄弟表示法作为存储结构，统计树中叶子结点的个数
 * */
public class CountLeafNodeByCBTree<AnyType> {
	
	public int countLeaf(CBNode<AnyType> rootNode){
		int leafLeft=0,leafRight=0;
		if(rootNode==null){
			return 0;
		}
		if(rootNode.getChildren()==null && rootNode.getBrother()==null){
			return 0;
		}
		leafLeft=countLeaf(rootNode.getChildren());
		leafRight=countLeaf(rootNode.getBrother());
		return leafLeft+leafRight+1;
	}
	
	//测试函数
	public static void main(String[] args){
		
		ChildrenBrother<Character> cb=new ChildrenBrother<Character>();
		Scanner in=new Scanner(System.in);
		System.out.println("请输入字符串，以先序的顺序创建创建二叉树：");
		//供测试数字串 :ABDH I  EJ K   CFL MP Q R    GN    
		String str=in.nextLine();
		/**将字符串转换成字符数组*/
		char[] tree=str.toCharArray();
		/**创建包装类的数组*/
		Character[] preOrder = new Character[tree.length];
		/**遍历数组，景数组中非空格的字符包装后，存入包装类数组，对空格字符赋值为null*/
		for(int i=0;i<tree.length;i++){
			if(tree[i]==' '){
				preOrder[i]=null;
			}else{
				preOrder[i]=tree[i];
			}
		}
		cb.rootNode=cb.createTreeByChildAndBrother(preOrder);
		
		CountLeafNodeByCBTree count = new CountLeafNodeByCBTree();
		int num=count.countLeaf(cb.rootNode);
		System.out.println("该树的叶子节点的个数为："+num);
		
	}	

}
